<?php

namespace app\index\controller;

use app\BaseController;
use frame\lib\Db;

class Login extends BaseController
{
    public function index()
    {
        $this->view();
    }
    public function doLog()
    {
        $switch = config('system', 'switch')['is_log'];
        if ($switch == 0) error('网页禁止登录，请联系管理员 !');
        $username = @$_POST['username'];
        $password = @$_POST['password'];
        if (empty($username) || empty($password)) error('缺少参数', 201);
        $data = Db::name('user')->where(['username' => $username, 'role' => 1])->find();
        if (!$data) error('找不到该用户数据');
        if (encrypt_pwd($password) != $data['password']) error('账号或密码错误');
        if ($data['status'] == 2) error('该账户已被封禁');
        $cook = ['username' => $username, 'id' => $data['id']];
        $_SESSION['user'] = $cook;
        // 登录时间保留7天
        Db::name('user')->where(['id' => $data['id']])->update(['last_login_time' => getTime()]);
        setcookie(session_name(), session_id(), time() + 3600 * 24 * 7, '/');
        success();
    }
    public function logOut()
    {
        if (@$_SESSION['user']) {
            $_SESSION['user'] = null;
            unset($_SESSION['user']);
            exit("<script>alert('退出成功');location.href='?p=index&c=login&a=index';</script>");
        }
    }
   
}
